define(function(require,exports,module){
    'use strict';

    var React = require('react');
    var $ = require('jquery');
    var bootstrap = require('bootstrap');
    var language = require('datetimepickerLanguage');
    var datetimepicker = require('datetimepicker');
    var publicObj = require('lib/public');
    var token = publicObj.get_token();

    //调用UserAreaDiagram组件
    var UserAreaDiagram = require('components/diagram/AreaBasicDiagram');
	var ReactLoading = require('../../partial/ReactLoading');
	
    module.exports = React.createClass({displayName: "exports",
        loadData:function(start_date,end_date){
            if(start_date && end_date){

                this.setState({'start_date':start_date});
                this.setState({'end_date':end_date});
                this.setState({'data':{'ios':[],'android':[]}});

                var sql = "SELECT FROM_UNIXTIME(ctime, '%Y-%m-%d') as name,\
                            count(*) as value\
                            FROM qeeniao.user\
                            where ctime > unix_timestamp('"+start_date+" 00:00:00')\
                            and ctime <= unix_timestamp('"+end_date+" 00:00:00')\
                            group by name\
                            order by name ASC";

                $.ajax({
                    url:this.props.url,
                    data:{sql:sql,'access_token':token},
                    type:'post',
                    dataType:'json',
                    success:function(o){
                        var tmp = [];
                        tmp.push(o);
                        if(o){
                            if(this.isMounted()) {
                                this.setState({'data': tmp});
                            }
                        }

                    }.bind(this),
                    error:function(xhr,status,err){
                        console.error(sql,status,err.toString());
                    }.bind(this)
                });
            }
        },
        handleSubmit:function(e){
            var start_date = React.findDOMNode(this.refs.from_date).value.trim();
            var end_date = React.findDOMNode(this.refs.to_date).value.trim();
			this.setState({'start':true});
            this.loadData(start_date,end_date);
        },
        componentDidMount:function(){
            $('#day_register_from_date').datetimepicker({
                language:'zh-CN',
                weekStart: 1,
                todayBtn:  1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2
            }).on('changeDate',function(ev){
                $('#day_register_to_date').datetimepicker('setStartDate',ev.date);
            }).on('outOfRange',function(){

            });

            $('#day_register_to_date').datetimepicker({
                language:'zh-CN',
                weekStart: 1,
                todayBtn:  1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2
            }).on('changeDate',function(ev){
                $('#day_register_from_date').datetimepicker('setEndDate',ev.date);
            }).on('outOfRange',function(){

            });
        },
        getInitialState:function(){
            return {
                'data':[],
                'start_date':'',
                'end_date':'',
				'start':false
            };
        },
        render:function(){
            var diagram;
            if(this.state.data.length > 0){
                diagram = this.state.data.map(function(item){
                    return (
                        React.createElement(UserAreaDiagram, {className: "", id: "MoreUserAreaDiagram", id_name: "MoreUserAreaDiagram", y: "注册量", title: "用户注册量", data: item})

                    );
                });
            }else{
				if(this.state.start){
					diagram = React.createElement(ReactLoading, {type: "spinning-bubbles", color: "#e3e3e3"})
				}
            }

            return (
                React.createElement("div", {className: "modal fade", id: this.props.id, tabindex: "-1", role: "dialog", "aria-labelledby": "myModalLabel", "aria-hidden": "true"}, 
                    React.createElement("div", {className: "modal-dialog"}, 
                        React.createElement("div", {className: "modal-content"}, 
                            React.createElement("div", {className: "modal-header"}, 
                                React.createElement("button", {type: "button", className: "close", "data-dismiss": "modal", "aria-label": "Close"}, React.createElement("span", {"aria-hidden": "true"}, "×")), 
                                React.createElement("h4", {className: "modal-title", id: "myModalLabel"}, "更多图表")
                            ), 

                            React.createElement("div", {className: "modal-body"}, 
                                React.createElement("div", {className: "container"}, 
                                    React.createElement("form", {className: "form-inline", id: "dateForm", onSubmit: this.handleSubmit}, 
                                        React.createElement("div", {className: "row"}, 
                                            React.createElement("div", {className: "form-group"}, 
                                                React.createElement("label", {htmlFor: "from_date"}, "选择日期："), 
                                                React.createElement("input", {onChange: this.handleStartDate, className: "form-control", size: "16", type: "text", value: this.state.start_date, id: "day_register_from_date", ref: "from_date", readOnly: "true", "data-date": "", "data-date-format": "yyyy-mm-dd", "data-link-field": "dtp_input2", "data-link-format": "yyy-mm-dd"})
                                            ), 

                                            React.createElement("div", {className: "form-group"}, 
                                                React.createElement("label", {htmlFor: "to_date"}, " - "), 
                                                React.createElement("input", {onChange: this.handleEndDate, className: "form-control", size: "16", type: "text", value: this.state.end_date, id: "day_register_to_date", ref: "to_date", readOnly: "true", "data-date": "", "data-date-format": "yyyy-mm-dd", "data-link-field": "dtp_input2", "data-link-format": "yyy-mm-dd"})
                                            ), 

                                            React.createElement("button", {type: "submit", style: {marginLeft:'10px'}, className: "btn btn-default btn-primary"}, "提交")
                                        )
                                    )
                                )
                            ), 

                            React.createElement("div", {className: "modal-body"}, 
                                React.createElement("div", {className: "row"}, 
                                    diagram
                                )
                            ), 

                            React.createElement("div", {className: "modal-footer"}, 
                                React.createElement("button", {type: "button", className: "btn btn-default", "data-dismiss": "modal"}, "退出")
                            )
                        )
                    )
                )
            );
        }
    });

});